package edu.sjsu.cs.queryopt.dbms.main;

import java.util.ArrayList;

public class MappingQueryCreate {
	

		public static final int NINE = 57;
		public static final int ZERO = 48;
	
	public static ArrayList<String> MappingCREATEQuery(String[] TokenizedQueryStringList)
	{
		ArrayList <String> orderInTable = new ArrayList<String>(); 
		int indexstep = 1;
		int numbeOfCol = 0;
//		for(int index = 0 ; index < TokenizedQueryStringList.length ; index++)
//		{
//			System.out.println( index +" "+ TokenizedQueryStringList[index]);
//			
//		}
		String TableNameForCreateQuery = "";
		String indexNameForCreateQuery = "";
//		System.out.println("Hello CEATE");
//		System.out.println(" Step 2 " + TokenizedQueryStringList[indexstep]);
		if(TokenizedQueryStringList[indexstep].equals(WordCapsule.QWC_INDEX))
		{
			indexstep++;
//			System.out.println(" Step 3 " + TokenizedQueryStringList[indexstep]);
			if(TokenizedQueryStringList[indexstep].equals(WordCapsule.QWC_EXTEN))
			{
				indexstep++;
				System.out.println("EXTENSION");
				
			}
			else if(Helper.IsIndexCorrect(TokenizedQueryStringList[indexstep]))
			{// this is table part need to check part
				indexstep++;
//				System.out.println(" Step 4 " + TokenizedQueryStringList[indexstep]);
				if((WordCapsule.QWC_ON).equals(TokenizedQueryStringList[indexstep]))
				{
					indexstep++;
//					System.out.println(" Step 5 " + TokenizedQueryStringList[indexstep]);
					if(Helper.IsTableNameCorrect(TokenizedQueryStringList[indexstep]))
					{
						indexstep++;
						numbeOfCol = Helper.howManyCols(TokenizedQueryStringList);
						
						if(numbeOfCol == 1)
						{// only one we only need "(" and ")"
							if(Helper.isParenthesesCorrcent(TokenizedQueryStringList, indexstep-1))
							{
								orderInTable.add(TokenizedQueryStringList[2]);
								orderInTable.add(TokenizedQueryStringList[4]);
								orderInTable.add(TokenizedQueryStringList[6]);
								return orderInTable;
								
							}
							else
							{
								System.out.println("isParenthesesCorrcent: Missing something");
								return null;
								
							}
							
						}
						else if(1 < numbeOfCol && numbeOfCol <= 5)
						{
//							System.out.println("Many Col" + "and i am " + TokenizedQueryStringList[indexstep]);
							if(Helper.isParenthesesCorrcent(TokenizedQueryStringList, indexstep-1))
							{
//								System.out.println("where am i ? " + indexstep);
								orderInTable.add(TokenizedQueryStringList[2]);
								orderInTable.add(TokenizedQueryStringList[4]);
								for(int indexcol = indexstep; indexcol < TokenizedQueryStringList.length ; indexcol++ )
								{
									if(TokenizedQueryStringList[indexcol].charAt(TokenizedQueryStringList[indexcol].length()-1) == WordCapsule.ACS )
									{
//										System.out.println("What is inside " +  TokenizedQueryStringList[indexcol]);
										orderInTable.add(TokenizedQueryStringList[indexcol]);
									}
									else if(TokenizedQueryStringList[indexcol].charAt(TokenizedQueryStringList[indexcol].length()-1) == WordCapsule.DCS)
									{
//										System.out.println("What is inside " +  TokenizedQueryStringList[indexcol]);
										orderInTable.add(TokenizedQueryStringList[indexcol]);
									}
									else
									{
										//System.out.println("This is not ACS OR DCS");
									}
									
								}
								
							}
							else
							{
								System.out.println("isParenthesesCorrcent: Missing something");
								return null;
								
							}
							
						}
						else
						{
							System.out.println("howManyCols : worng number not 1,2,3,4,5 ");
							return null;
							
						}
						
					}
					else
					{
						System.out.println("IsIndexCorrect is is not table name");	
						return null;
					}
					
				}
				else
				{
					System.err.println("MappingCREATEQuery: this is not ON");
					return null;
					
				}
				
				
			}
			else{
				System.err.println("MappingCREATEQuery: It doesn't contain index number ");
				return null;
				
			}
			
			
		}
		else
		{
			System.err.println("MappingCREATEQuery:  It doesn't start with INDEX");
			return null;
			
		}
		return orderInTable;

	}

//	
//	public static int AfterCreateWord(String[] FuckingQuery)
//	{
//
//		
//	}
//	
//	
	
	
	/**
	 * getTableName
	 * @param tablename: table name + index name
	 * How?
	 * First, if first char is "T" then go to ge table name o/w do nothing
	 * Second, String add T in to String Tablename then go to while loop
	 * Third, if tablename hit the "I" STOP other while it is appeading table name
	 * 		 to TableName var
	 * **/

//	public static String getIndexName(String indexName)
//	{
//		String IndexName = "";
//		int index = 0;
//		
//		while(true)
//		{
//			
//			if(indexName.charAt(index) == INDEX)
//			{
//				IndexName += indexName.charAt(index++); /**insert X **/
//
//				for(int innerIndex = index ; innerIndex < indexName.length(); innerIndex++)
//				{
//					
//					if(((int)indexName.charAt(innerIndex)<= NINE) && 
//							((int)indexName.charAt(innerIndex)>= ZERO) )
//					{					
//						IndexName += indexName.charAt(innerIndex);
//					}
//					else
//					{
//						System.out.println("This is not number try again");
//						
//					}
//				}
//				System.out.println("Index name is : " + IndexName);
//				return 	IndexName;
//			}
//			index++;
//
//			
//		}
//	}
//	public static ArrayList<String> OrderOfCols(String[] order)
//	{
//		int howManyColInthisQuery = howManyCols(order);
//		ArrayList<String> al  = new ArrayList<String>();
//		
//		for(int index = 0 ; index < order.length ; index++)
//		{
//			if(order[index].equals("("))
//			{
//				for(int index2 = index+1 ; index < order.length ; index++)
//				{
//					String aORd = "";
//					aORd = order[index2];
//					if(order[index2].charAt(aORd.length()-1) == 'A')
//					{
//						al.add("A");
//						
//					}
//					else if(order[index2].charAt(aORd.length()-1) == 'D')
//					{
//						al.add("D");
//						
//					}
//					else
//					{
//						System.out.println("OrderOfCols: It is not A or D");
//						
//					}
//					
//				}
//				
//			}
//			
//		}	
//		if(howManyColInthisQuery  != al.size())
//		{
//			
//			System.out.println("OrderOfCols: there is problem in the number of col");
//		}
//		return al;
//	}
//	
//	
//	

	
	
	

}
